﻿using System;
using System.Collections.Generic;
using GameFramework.common;
using GameFramework.UI;
using UnityEngine;

namespace GameFramework.events
{
    
    // V --> P --> M
    // V <-- P <-- M
    //上面为消息的流转方向，MVP之间只用Action来传递消息，不直接引用,可以看出一次通信需要4个Action，代码复杂度还是很高的
    public class MVPEventManager:Singleton<MVPEventManager>
    {
        //M-->V
        private Dictionary<Type,Action<object>> viewModelPreparedActions = new Dictionary<Type, Action<object>>();
        //V-->M
        private Dictionary<Type,Action> viewShowActions = new Dictionary<Type, Action>();

        public void onViewShowListener<T>(Action action)
        {
            Type type = typeof(T);
            if (!typeof(IView).IsAssignableFrom(type))//不是view实例
            {
                return;
            }
            
            if (viewShowActions.ContainsKey(type))
            {
                viewShowActions[type]+=action;
            }
            else
            {
                viewShowActions[type] = action;
            }
        }
        public void onViewModelPreparedListener(Type viewType, Action<object> refreshAction)
        {
            Type type = viewType;
            if (!typeof(IView).IsAssignableFrom(type))//不是view实例
            {
                return;
            }
            
            if (viewModelPreparedActions.ContainsKey(type))
            {
                viewModelPreparedActions[type]+=refreshAction;
            }
            else
            {
                viewModelPreparedActions[type] = refreshAction;
            }
        }
        public void onViewShow(Type viewType)
        {
            Type type = viewType;
            if (!typeof(IView).IsAssignableFrom(type))//不是view实例
            {
                return;
            }
            
            if (viewShowActions.ContainsKey(type))
            {
                viewShowActions[type]?.Invoke();
            }
            else
            {
                Debug.LogWarning($"{type.Name} 没有对应modle注册show事件！");
            }
        }
        public void onViewModelPrepared(Type viewType,object data)
        {
            Type type = viewType;
            if (!typeof(IView).IsAssignableFrom(type))//不是view实例
            {
                return;
            }
            
            if (viewModelPreparedActions.ContainsKey(type))
            {
                viewModelPreparedActions[type]?.Invoke(data);
            }
            else
            {
                Debug.LogWarning($"{type.Name} 没有注册refresh事件！");
            }
        }

        #region LoadingView

        public  Action LoadinView_V2P_Show;//view显示后通知
        #endregion

        #region SetNamingView

        public  Action<string> SetNamingView_V2P_SetName;//用户输入用户名确认后

        #endregion

        #region HomeView

        public  Action HomeView_V2P_MainPlayerShow;//界面主角色显示
        public  Action HomeView_V2P_MainPlayerHide;

        #endregion

        #region LoadingView
        
        public  Action<float> SceneLoadingView_ProcessChange;//加载进度显示
        #endregion

    }
}